<?php
namespace app\admin\controller\store;
use think\Db;
class DefaultGoods extends \app\admin\controller\Common
{
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\DefaultGoods())
                ->field([
                    'id','title','image','cate_id','sort'
                ])->append([]);

        isset($search['title']) && $search['title'] != "" ? $query->where("title like '%{$search['title']}%'") : '';

        $query->order("id desc");
        $list = $query->limit($page, $limit)->select();
        $count = $query->count();
   
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            (new \app\common\model\DefaultGoods())->save($data);
            return self::successResult();
        } else {
            $this->assign("type_arr",(new \app\common\model\Version())->tpye_arr());
            return view();
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param();
            // $validate = new \app\common\validate\Menu();
            // $result = $validate->check($data);
            // if ($result === false) {
            //     $this->error($validate->getError());
            // }
            $table = new \app\common\model\DefaultGoods();
            $row = $table->allowField(true)->save($data, ['id' => $data['id']]);
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\DefaultGoods())->where(['id' => $id])->find();
            $this->assign('info',$info);
   
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\DefaultGoods();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }

    public function static(){
        
    }
  

 
}
